﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace quanlyktx.quanlysinhvien
{
    public partial class frmsinhvien : DevComponents.DotNetBar.Office2007Form 
    {
        public frmsinhvien()
        {
            InitializeComponent();
        }
        ketnoi kn = new ketnoi();
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable("lop");
        DataTable dtp = new DataTable("phong");
        private void btthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void groupPanel2_Click(object sender, EventArgs e)
        {

        }
        private void laylop()
        {
            SqlConnection cn = kn.taokn();
            cn.Open();
            SqlCommand cmd = new SqlCommand("lop_SelectAll", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            da.SelectCommand = cmd;
            da.Fill(dt);
            cblop.DataSource = dt;
            cblop.DisplayMember = "tenlop";
            cblop.ValueMember = "malop";
        }
        private void layphong()
        {
            SqlConnection cn = kn.taokn();
            cn.Open();
            SqlCommand cmdp = new SqlCommand("phong_SelectAll", cn);
            cmdp.CommandType = CommandType.StoredProcedure;
            da.SelectCommand = cmdp;
            da.Fill(dtp);
            cbphong .DataSource = dtp;
            cbphong .DisplayMember = "tenphong";
            cbphong .ValueMember = "maphong";
        }
        private void btthem_Click(object sender, EventArgs e)
        {
            btsua.Enabled = false;
            btxoa.Enabled = false;
            btin.Enabled = false;
            bttimkiem.Enabled = false;
            txttimkiem.Enabled = false;
            if (btthem.Text == "Thêm")
            {
                //laydltext();
                SqlConnection cn = kn.taokn();
                cn.Open();
                SqlCommand cmd = new SqlCommand("masvtutang", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    txtmasv.Text = dr[0].ToString();
                }
                txtsdt.Text = "";
                txttensv.Text = "";
                txtdiachi.Text = "";
                txtcmnd.Text = "";
                btthem.Text = "Cập nhật";
               
            }
            else
            {
                if (txttensv.Text == "" || txtcmnd.Text == "" || txtdiachi.Text == "" || txtnhanvien.Text == "")
                {
                    MessageBox.Show("dữ liệu ko dược để trống", "lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SqlConnection cn = kn.taokn();
                    cn.Open();
                    SqlCommand cmd = new SqlCommand("sinhvien_Insert", cn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@masv", SqlDbType.NVarChar).Value = txtmasv.Text;
                    cmd.Parameters.Add("@tensv", SqlDbType.NVarChar).Value = txttensv.Text;
                    cmd.Parameters.Add("@ngaysinh", SqlDbType.DateTime).Value = dtngaysinh.Value;
                    bool gt;
                    if (rbnam.Checked == true)
                    {
                        gt = true;
                    }
                    else gt = false;
                    cmd.Parameters.Add("@gioitinh", SqlDbType.NVarChar).Value = gt;
                    cmd.Parameters.Add("@socmnd", SqlDbType.Int).Value = txtcmnd.Text;
                    cmd.Parameters.Add("@diachi", SqlDbType.NVarChar).Value = txtdiachi.Text;
                    cmd.Parameters.Add("@sdt", SqlDbType.Int).Value = txtsdt.Text;
                    // cmd.Parameters.Add("@makhoa", SqlDbType.Int).Value = cbkhoa.Text;
                    cmd.Parameters.Add("@malop", SqlDbType.NVarChar).Value = cblop.Text;
                    cmd.Parameters.Add("@manv", SqlDbType.NVarChar).Value = txtnhanvien.Text;
                    cmd.Parameters.Add("@ngayvao", SqlDbType.DateTime).Value = dtngayvao.Value;
                    cmd.Parameters.Add("@ngayra", SqlDbType.DateTime).Value = dtngayra.Value;
                    cmd.Parameters.Add("@anh", SqlDbType.NVarChar).Value = anhsinhvien;
                    cmd.ExecuteNonQuery();
                    cmd.Dispose();
                    MessageBox.Show("bạn đã thêm thành công", "thông báo");
                    btthem.Text = "Thêm";
                    btsua.Enabled = true;
                    btxoa.Enabled = true;
                    btin.Enabled = true;
                    bttimkiem.Enabled = true;
                    txttimkiem.Enabled = true;
                }
            }
        }

        private void btsua_Click(object sender, EventArgs e)
        {
            btthem.Enabled = false;
            btxoa.Enabled = false;
            bttimkiem.Enabled = false;
            txttimkiem.Enabled = false;
            btin.Enabled = false;
            if (btsua.Text == "Sửa")
            {
                laydltext();
                btsua.Text = "Lưu";
            }
            else
            {
                if (txttensv.Text == "" || txtcmnd.Text == "" || txtdiachi.Text == "" || txtnhanvien.Text == "")
                {
                    MessageBox.Show("dữ liệu ko dược để trống", "lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SqlConnection cn = kn.taokn();
                    cn.Open();
                    SqlCommand cmd = new SqlCommand("sinhvien_Update", cn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@masv", SqlDbType.NVarChar ).Value = txtmasv.Text;
                    cmd.Parameters.Add("@tensv", SqlDbType.NVarChar).Value = txttensv.Text;
                    cmd.Parameters.Add("@ngaysinh", SqlDbType.DateTime).Value = dtngaysinh.Value;
                    bool gt;
                    if (rbnam.Checked == true)
                    {
                        gt = true;
                    }
                    else gt = false;
                    cmd.Parameters.Add("@gtsv", SqlDbType.NVarChar).Value = gt;
                    cmd.Parameters.Add("@socmnd", SqlDbType.Int).Value = txtcmnd.Text;
                    cmd.Parameters.Add("@diachi", SqlDbType.NVarChar).Value = txtdiachi.Text;
                    cmd.Parameters.Add("@sdt", SqlDbType.Int).Value = txtsdt.Text;
                    // cmd.Parameters.Add("@makhoa", SqlDbType.Int).Value = cbkhoa.Text;
                    cmd.Parameters.Add("@malop", SqlDbType.NVarChar ).Value = cblop.Text;
                    cmd.Parameters.Add("@manv", SqlDbType.NVarChar ).Value = txtnhanvien.Text;
                    cmd.Parameters.Add("@ngayvao", SqlDbType.DateTime).Value = dtngayvao.Value;
                    cmd.Parameters.Add("@ngayra", SqlDbType.DateTime).Value = dtngayra.Value;
                    cmd.Parameters.Add("@anh", SqlDbType.NVarChar).Value = anhsinhvien;
                    cmd.ExecuteNonQuery();
                    cmd.Dispose();
                    MessageBox.Show("bạn đã thêm thành công", "thông báo");
                    btsua.Text = "Sửa";
                    btthem.Enabled = true;
                    btin.Enabled = true;
                    bttimkiem.Enabled = true;
                    btxoa.Enabled = true;
                }
            }
        }

        private void btxoa_Click(object sender, EventArgs e)
        {
            SqlConnection cn = kn.taokn();
            cn.Open();
            SqlCommand cmd = new SqlCommand("sinhvien_Delete", cn);
            if (MessageBox.Show("bạn có thực sự muốn xóa sinh viên:" + txtmasv .Text + "này ", "thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@masv", SqlDbType.NVarChar ).Value = txtmasv .Text;
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                MessageBox.Show("bạn đã xóa thành công");

            }
        }

        private void bttimkiem_Click(object sender, EventArgs e)
        {
            SqlConnection cn = kn.taokn();
            cn.Open();
            SqlCommand cmd = new SqlCommand("sinhvien_GetByID", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@masv", SqlDbType.NVarChar).Value = txttimkiem.Text;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            cn.Close();
        }
        private void laydltext()
        {
            txtmasv.DataBindings.Clear();           
            txtmasv.DataBindings.Add("Text", dgsinhvien.DataSource, "masv");
            txttensv.DataBindings.Clear();
            txttensv.DataBindings.Add("Text", dgsinhvien.DataSource, "tensv");
            dtngaysinh.DataBindings.Clear();
            dtngaysinh.DataBindings.Add("Text", dgsinhvien.DataSource, "ngaysinh");
            txtcmnd.DataBindings.Clear();
            txtcmnd.DataBindings.Add("Text", dgsinhvien.DataSource, "socmnd");
            txtsdt.DataBindings.Clear();
            txtsdt.DataBindings.Add("Text", dgsinhvien.DataSource, "sdt");
            txtnhanvien.DataBindings.Clear();
            txtnhanvien.DataBindings.Add("Text", dgsinhvien.DataSource, "manv");
            txtdiachi.DataBindings.Clear();
            txtdiachi.DataBindings.Add("Text", dgsinhvien.DataSource, "diachi");
            cblop.DataBindings.Clear();
            cblop.DataBindings.Add("Text", dgsinhvien.DataSource, "malop");
            cbphong.DataBindings.Clear();
            cbphong.DataBindings.Add("Text", dgsinhvien.DataSource, "maphong");
            rbnam .DataBindings.Clear();
            rbnam .DataBindings.Add("Text", dgsinhvien.DataSource, "gtsv");
            if (rbnam .Text == "False")
            {
                rbnam .Text = "Nam";
                rbnam .Checked = true;
                rbnu .Text = "Nữ";
            }

            else
            {
                rbnu .Text = "Nữ";
                rbnu .Checked = true;
                rbnam .Text = "Nam";
            }
           
        
        }
        private void frmsinhvien_Load(object sender, EventArgs e)
        {
            laylop();
            layphong();
            string sql = "sinhvien_SelectAll";
            dgsinhvien.DataSource = kn.taobang(sql);
           

        }
        string anhsinhvien;
        private void llabanh_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            OpenFileDialog dl = new OpenFileDialog();
            dl.InitialDirectory = Application.StartupPath + @"anhsinhvien/";
            if (dl.ShowDialog() == DialogResult.OK)
            {
                anhsinhvien  = dl.FileName.Substring(dl.FileName.LastIndexOf("\\") + 1, dl.FileName.Length - dl.FileName.LastIndexOf("\\") - 1);
                ptanh.Image = new Bitmap(Application.StartupPath + @"\anhsinhvien\" + anhsinhvien );
                ptanh .SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }
    }
}
